home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 489 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.8 KB  |  152 lines

  1. Path: news.sprintlink.net!datalytics!news
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: ode to c++
  5. Date: 4 Jan 1996 19:00:26 GMT
  6. Organization: Datalytics, Inc
  7. Message-ID: <4ch84a$ldg@gold.datalytics.com>
  8. References: <4bq620$p1t@news.iconn.net>
  9. NNTP-Posting-Host: pc071.datalytics.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. thecrow@iconn.net (The Crow) wrote:
  16. >About 2 months ago I got very into Pascal (because it was the only thing on the 
  17. >network at my highschool) I wrote some pretty cool stuff with it.  I also 
  18. >messed around with VB and found it to be FAR too slow at number crunching.  
  19. >Then I finally sat down with C++ these last two days.  I rewrote a simple 
  20. >Pascal program in C++ that calculates Pythagorean triples, here is the C++ 
  21. >version
  22. >
  23. >//    The amazing Pythagorean Triple Generator
  24. >#include <stdio.h>
  25. >
  26. >main()
  27. >{
  28. >const     deep = 13200;
  29. >long      a = 1,b = 1,c = 1;
  30. >
  31. >    for(c = 1; c<= deep; c++)
  32. >    {
  33. >        for(b = c/2; b <= c; b++)
  34. >        {
  35. >            for(a = (c-b)+1; a <= b; a++)
  36. >            {
  37. >                if (a*a + b*b > c * c) break;
  38. >                if (a*a + b*b == c * c)
  39. >                printf("%ld %ld %ld \n",a,b,c);
  40. >            } // for a
  41. >        }    // for b
  42. >    }    // for c
  43. >}    // main
  44. >
  45. >
  46. >The Pascl version took 20 seconds for C to get to 500, C++ took 6 seconds! It 
  47. >blew my mind!, I'm never going back! long live C!
  48. >
  49. >
  50. >-- 
  51. >The Crow - thecrow@iconn.net
  52. >"It can't rain all the time"
  53. >-Kryptology
  54. >
  55.  
  56. Now you know why so many people prefer C and C++.  They aren't 
  57. the easiest languages to learn, but they are screamers.  Now, 
  58. two points of order.  First, you wrote a C program.  Without 
  59. resorting to OOP, I'll show you how to take advantage of some 
  60. better things you can do with C++.  Second, you are falling 
  61. into some stylistic traps that I would like to correct.
  62.  
  63. The code below illustrates both points:
  64.  
  65. #include <assert.h>      // assert
  66. #include <iostream.h>    // cout
  67. #include <math.h>        // sqrt
  68. #include <stddef.h>      // stddef.h
  69.  
  70. typedef unsigned long index;
  71.  
  72. int main(void)
  73. {
  74.    const int DEEP = 13200;
  75.    const index maxIndex = sqrt(ULONG_MAX);
  76.    for (index c = 1; c <= DEEP; c++)
  77.    {
  78.       for (index b = c / 2; b <= c; b++)
  79.       {
  80.          for (index a = (c - b) + 1; a <= b; a++)
  81.          {
  82.             assert(a < maxIndex);
  83.             index a2 = a * a;
  84.             assert(b < maxIndex);
  85.             index b2 = b * b;
  86.             index sumOfSquares = a2 + b2;
  87.             assert(c < maxIndex);
  88.             index c2 = c * c;
  89.             if (sumOfSquares > c2)
  90.             {
  91.                break;
  92.             }
  93.             if (sumOfSquares == c2)
  94.             {
  95.                cout << a << ' ' << b << ' ' << c << ' '
  96.                   << endl;
  97.             }
  98.          }
  99.       }
  100.    }
  101.    return 0;
  102. }
  103.  
  104. Note that I change the case of "deep."  Making it uppercase 
  105. makes its being const stylistically obvious.  Note also that I 
  106. declared and initialized the for loop indexes in the for loop 
  107. statements.  At the same time, I changed their type to size_t.   
  108. This is a standard typedef to the native type for the current 
  109. environment.  It is usually a good choice for loop indices.  
  110. (On the other hand, for some environments, it could prove too 
  111. small for your purposes.)
  112.  
  113. Note that you must be certain of the capacity of the type you 
  114. use for the loop indexes.  You used a signed type for an 
  115. unsigned value, so you immediately lost half the capacity to 
  116. the sign bit.  Furthermore, squaring the indexes could exceed 
  117. the capacity of the type you selected.  And, of course, adding 
  118. the square of a to the square of b could exceed the capacity.  
  119. As originally written, your program would simply provide 
  120. incorrect answers because the values in the conditionals would 
  121. wrap to small numbers.
  122.  
  123. I used the assert macro to report a value out of range and 
  124. terminate the program.  The assert macro conditional compiles 
  125. to a NOP if you #define NDEBUG.
  126.  
  127. I also added whitespace around all binary operators.  This 
  128. makes the code a little more readable.  I also removed the 
  129. duplicate calculations in the inner for loop by creating a 
  130. couple of temporaries.  (Do note the identifiers I chose a 
  131. reasonably indicative of what they represent.)
  132.  
  133. Finally, I wrote the if statements in the block style for 
  134. consistency.  Many old C programmers still use the (perfectly 
  135. legal, but harder to read and more error prone) style you 
  136. used.  However, as soon as the conditional requires a second 
  137. statement, you must use the block style.  Without the block 
  138. style you can get confused as to what belongs to the 
  139. conditional and what doesn't.  (This has been the subject of 
  140. PC-Lint advertisements for years.)
  141.  
  142. My stylistic comments are subjective, to be sure, but well 
  143. conceived and time-tested.
  144.  
  145. -- 
  146. Robert Stewart        | My opinions are usually my own.
  147. Datalytics, Inc.
  148. (513)226-7700
  149. stew@datalytics.com
  150.  
  151.  
  152.